/*
Ensure crossbrowser foreach support
source: http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Global_Objects:Array:forEach
*/

if (!Array.prototype.forEach)
{
  Array.prototype.forEach = function(fun /*, thisp*/)
  {
    var len = this.length;
    if (typeof fun != "function")
      throw new TypeError();

    var thisp = arguments[1];
    for (var i = 0; i < len; i++)
    {
      if (i in this)
        fun.call(thisp, this[i], i, this);
    }
  };
}

/*
	SimplyPost Toggle Class
*/

var Toggle = new Class({
	initialize: function(obj, commandObj, on_txt, off_txt) {
		this.obj = $(obj);
		this.on_txt = on_txt || 'On';
		this.off_txt = off_txt || 'Off';

		this.on_comm = commandObj.on;
		this.off_comm = commandObj.off;

		var that = this;
		this.obj.addEvent('click', function() {
			that.toggle();
		});
	},

	toggle: function() {
		if (this.obj.hasClass('on')) {
			this.off();
			this.obj.toggleClass('on');
			this.obj.toggleClass('off');
			this.obj.setHTML(this.off_txt);
		}
		else {
			this.on();
			this.obj.toggleClass('off');
			this.obj.toggleClass('on');
			this.obj.setHTML(this.on_txt);
		}
	},

	on: function() {
		this.on_comm();
	},

	off: function() {
		this.off_comm();
	}
});


/*
	SimplyPost Popup Window Class
*/
var Popup = new Class({
	
	initialize: function() {
		this.current;
		this.el;
		this.content;
		this.containerClass = 'sp_container';
	},
	setHTML: function() {
		this.current.innerHTML = '';
		if (typeof(this.content) == 'string')
			this.current.innerHTML = this.content;
		else
			this.current.appendChild(this.content);
		this.current.setProperty('class', this.containerClass);
	},
	addEvent: function() {
		that = this;
		document.body.addEvent('keydown', function(e) {
			var key = e.keyCode;
			if (key == 27)
				that.remove();
		});
	},
	create: function() {
		var obj = new Element('div', { 'class':this.containerClass });
		if (typeof(this.content) == 'string')
			obj.innerHTML = this.content;
		else
			obj.appendChild(this.content);
		this.current = obj;
		document.body.appendChild(this.current);
		this.current.style.position = 'absolute';		
	},
	show: function(el, cont, contClass) {
		this.containerClass = contClass || 'sp_container';
		this.options = Object.extend({
			offsetLeft: 0,
			offsetTop: 0
		}, arguments[3] || { });

		this.el = el || document.body;
		this.content = cont || 'test';			
		if (!this.current)
			this.create();
		else
			this.setHTML();

		pos = this.el.getPosition();		
		this.current.style.top = pos.y + this.options.offsetTop + 'px';
		this.current.style.left = pos.x + this.options.offsetTop + 'px';
		this.addEvent();
	},
	remove: function() {
		this.current.remove();
		delete this.current;
	}
});

/*
	SimplyPost Inline Editor Class
*/
var Editor = new Class({

	initialize: function(el) {

		this.options = Object.extend({
			url: 		'',
			fieldName: 	'field',
			id: 		el.id,
			parameters: { }
		}, arguments[1] || { });
		
		this.css = Object.extend({
			contClass: 'editor',
			formClass: 'editor_form',
			fieldClass: 'editor_field'
		}, arguments[2] || { });
	
		this.el = el;
		this.content = el.innerHTML;
		this.container = SP_Global.Pop;
	},
	submit: function(e) {
		var form = e.toQueryString();
		var url = this.options.url+this.options.id;
		form += '&ajax=true';
		var that = this;
		
		new Ajax(url, {
			method: 'post',
			data: form,
			onSuccess: function(resp) {
				that.el.setHTML(resp);
				that.container.remove();
			},
			onFailure: function(resp) {
				var error = ajax_failure(error);
				that.container.setHTML(error);
			}
		}).request();
	},
	addEvents: function(form) {
		var that = this;
		form.addEvent('submit', function(e) {
			e = new Event(e);
			e.stop();
			that.submit(this);
		});
		form.addEvent('reset', function(e) {
			that.remove();
		});
	},
	show: function() {		
		this.content = this.el.innerHTML.trim();
		
		var	cont =	new Element('div', { 'class':this.css.contClass });
		var form = new Element('form', { method:'post', id:'sp_form', 'class':this.css.formClass });
		var input = new Element('input', { type:'text', name: this.options.fieldName, value:this.content, id:'sp_edit_field' });
		var setHTML = new Element('input', { type:'submit', value:'Update', id:'update_link' });
		var cancel = new Element('input', { type:'reset', value:'Cancel', id:'cancel_link' });
		[input, setHTML, cancel].each(function(e) { form.appendChild(e) });
		cont.appendChild(form);
		
		this.container.show(this.el, cont);
		this.addEvents(form);
		input.select();
	},
	remove: function() {
		this.container.remove();
	}
});

/*
	SimplyPost Mulit-line Editor Class - extends Editor
*/

var BigEditor = Editor.extend({
	show: function() {				
		this.content = this.el.innerHTML;
		
		var	cont =	new Element('div', { 'class':this.css.contClass });
		var form = new Element('form', { method:'post', id:'sp_form', 'class':this.css.formClass });
		var input = new Element('textarea', { name: this.options.fieldName, id:'sp_edit_field' });
		var update = new Element('input', { type:'submit', value:'Update', id:'update_link' });
		var cancel = new Element('input', { type:'reset', value:'Cancel', id:'cancel_link' });
		input.innerHTML = this.content;
		
		[input, update, cancel].each(function(e) { form.appendChild(e) });
		cont.appendChild(form);
		
		this.container.show(this.el, cont);
		
		this.addEvents(form);
	},
});

var Confirm = Editor.extend({
	addEvents: function() {
		that = this;
		$('cancel_link').addEvent('click', function(e) {
			e = new Event(e);
			e.stop();
			that.remove();
		});
		$('confirm_link').addEvent('click', function(e) {
			that.remove();  //just a filler - we're not stopping the link
		});
	},
	show: function() {
		var conf = new Element('div', { 'class':'confirm' });
		var msg = new Element('p');
		var confirm = new Element('a', { id:'confirm_link', href:this.el.href });
		var cancel = new Element('a', { id:'cancel_link', href:'#' });
		msg.innerHTML = 'Are you sure?';
		confirm.innerHTML = 'Yes';
		cancel.innerHTML = 'No';
		[msg, confirm, cancel].each(function(e) { conf.appendChild(e) });

		var offLeft = -20;
		var offTop = -20;

		this.container.show(this.el, conf, 'confirm_container', { offsetLeft: offLeft, offsetTop:offTop });
		this.addEvents();
	}
});

createEditor = function(css, params) {
	var ret = [];
	$$(css).each(function(el) {
		var edit = new Editor(el, params);
		ret.push({ dom_handle:el, editor:edit });
	});
	return ret;
}


createBigEditor = function(css, params) {
	var ret = [];
	$$(css).each(function(el) {
		var edit = new BigEditor(el, params);
		ret.push({ dom_handle:el, editor:edit });
	});
	
	return ret;
}


//Define Globals - only want one popup per page
var SP_Global = {
	Pop: new Popup()
};